package com.polidea.rxohosble2.sample;

import com.polidea.rxohosble2.internal.RxBleLog;
import com.polidea.rxohosble2.sample.example2_connection.ConnectionExampleAbility;
import com.polidea.rxohosble2.sample.example3_discovery.ServiceDiscoveryExampleAbility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class DeviceAbility extends Ability {
    public static final String EXTRA_MAC_ADDRESS = "extra_mac_address";

    public String getMacAddress() {
        return macAddress;
    }

    public String macAddress;

    public void onConnectClick(Component component) {
        RxBleLog.i("DeviceAbility onConnectClick");
        final Intent intent = new Intent();
        intent.setParam(EXTRA_MAC_ADDRESS,macAddress);
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ConnectionExampleAbility.class)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    public void onDiscoveryClick(Component component) {
        RxBleLog.i("DeviceAbility onDiscoveryClick");
        final Intent intent = new Intent();
        intent.setParam(EXTRA_MAC_ADDRESS,macAddress);
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ServiceDiscoveryExampleAbility.class)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setPattern(ResourceTable.Pattern_base);
        super.setUIContent(ResourceTable.Layout_ability_device);
        findComponentById(ResourceTable.Id_connect).setClickedListener(this::onConnectClick);
        findComponentById(ResourceTable.Id_discovery).setClickedListener(this::onDiscoveryClick);
        macAddress = intent.getStringParam(EXTRA_MAC_ADDRESS);
        ((Text) findComponentById(ResourceTable.Id_title)).setText(getString(ResourceTable.String_mac_address, macAddress));
    }
}
